<template>
  <div class="book-detail">
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ name: 'index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ name: 'bookCenter' }">图书中心</el-breadcrumb-item>
      <el-breadcrumb-item>{{ book.name }}</el-breadcrumb-item>
    </el-breadcrumb> -->
    <div class="b-title">
      <div class="cover">
        <a href="javascript:;" v-if="book.book">
          <t-img :src="bindIMG(book.book.filename)" alt="" customClass="img" />
        </a>
      </div>
      <div class="info">
        <div class="title">
          {{ book.name }}
          <small class="author">{{ book.author }}</small>
        </div>
        <div class="description">
          {{ book.description }}
        </div>
      </div>
      <div class="btns">
        <a href="javascript:;" class="borrow" :class="{ 'ban': currentBorrowState ? currentBorrowState.id !== 4 : false }"
          @click="borrowBook">{{
            currentBorrowState ? currentBorrowState.value : '我要借阅' }}</a>
        <a href="javascript:;" class="read"
          :class="{ 'ban': !currentBorrowState || (currentBorrowState.id !== 2 && currentBorrowState.id !== 3) || chapters.length === 0 }"
          @click="readBook(defaultChapter)">在线阅读</a>
      </div>
    </div>
    <div class="b-chapters">
      <template v-if="chapters.length">
        <h4>
          章节列表 <small>共{{ chapters.length }}章</small>
        </h4>
        <ul class="c-list" :class="{ 'mask': currentBorrowState ? currentBorrowState.id !== 2 : true }">
          <li class="c-item" v-for="item in chapters" :key="item.id">
            <a href="javascript:;" @click="readBook(item)">
              {{ item.indexs | ch }} {{ item.title }}</a>
          </li>
        </ul>
      </template>
      <template v-else>
        <div class="not-found" style="margin-top:20px;">
          本书暂无章节
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from 'vuex';
import { bindIMG } from '@utils';
import mixin from '@mixins';
import { orderState, borrowState } from '@static';

export default {
  mixins: [mixin.home],
  props: ['id'],
  data () {
    return {
      list: [],
      orderState,
      borrowState,
      chapters: [],
      defaultChapter: ''
    };
  },
  computed: {
    ...mapState({
      book: 'currentBook',
      user: 'currentUser',
      myBorrow: 'myBorrow'
      // userOrder: 'userOrder'
    }),
    currentBorrow () {
      return Array.isArray(this.myBorrow) ? this.myBorrow?.find((item) => item.bookId === this.id && item.state !== 4) : undefined
    },
    currentBorrowState () {
      return borrowState.find(item => item.id === this.currentBorrow?.state)
    }
  },
  methods: {
    ...mapActions(['fetchUserOrder', 'fetchMyBorrow']),
    ...mapMutations(['setCurrentChapter']),
    bindIMG,
    async fetchChapter () {
      const chapters = await this.$api.getChapterById({ bookId: this.id });
      this.chapters = chapters.sort((a, b) => {
        return a.indexs - b.indexs;
      });
      this.defaultChapter = this.chapters[0] || {};
    },

    readBook (data) {
      this.setCurrentChapter(data);
      this.$router.push({
        name: 'bookChapter',
        params: { id: this.id, cid: data.id }
      });
    },
    borrowBook () {
      this.handleConfirm('借阅图书', async () => {
        const { success } = await this.$api.addBorrow({
          bookId: this.book.id,
          bookName: this.book.name,
          createTime: Date.now(),
          updateTime: Date.now(),
          state: 0,
          userId: this.user.id,
          userName: this.user.name
        })
        if (success) {
          this.$message.success('成功提交...等待管理员审核~')
          this.fetchMyBorrow()
        } else {
          this.$message.error('提交失败...')
        }
      })
    }
  },
  async mounted () {
    await this.fetchMyBorrow()
    this.fetchChapter();
  }
};
</script>

<style lang="less" scoped>
.book-detail {
  background-color: #fff;
  padding: 10px 20px;
}

/deep/.el-breadcrumb__item {
  span {
    font-size: 14px;
    line-height: 18px;
    font-weight: normal;
  }
}

.b-title {
  display: flex;
  margin-top: 20px;
  padding-left: 20px;
  padding-right: 20px;
}

.cover {
  ::v-deep .img {
    width: 200px;
  }
}

.info {
  flex: 1;
  padding-left: 20px;
  position: relative;

  .title {
    font-size: 20px;
    font-weight: 700;

    .author {
      color: #576574;
      font-weight: 400;
      font-size: 14px;
      margin-left: 10px;
    }
  }

  .description {
    height: 100%;
    font-size: 14px;
    line-height: 1.5;
    padding: 20px 0;
    color: #111;
  }

}

.btns {
  display: flex;
  flex-direction: column;
  gap: 16px;

  a {
    display: inline-block;
    padding: 6px 20px;
    border: 1px solid #000;
    border-radius: 20px;
    text-align: center;
    transition: all 0.3s ease-out;
  }

  .borrow {
    border-color: #eba613;
    color: #eba613;

    &:hover {
      background-color: #eba613;
      color: #fff;
    }
  }

  .read {
    border-color: #27ae60;
    background-color: #27ae60;
    color: #fff;

    &:hover {
      background-color: #eba613;
    }
  }
}

.b-chapters {
  h4 {
    font-size: 20px;
    padding-left: 10px;
    border-left: 3px solid #eba613;
    margin: 40px 0 20px;
  }

  .c-list {
    display: flex;
    border: 1px solid #f0f0f0;
    padding: 40px;

    .c-item {
      width: 33.33%;
      // text-align: center;
      padding-bottom: 10px;
      border-bottom: 1px solid #f0f0f0;

      a {
        &:hover {
          color: #eba613;
        }
      }
    }

    &.mask {
      position: relative;
      background-color: #eceeff;

      &::before {
        position: absolute;
        content: "请先借阅";
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: #eba613;
        font-size: 20px;
      }

      a {
        pointer-events: none;
        color: #999;
      }
    }
  }
}

// done ban click
.ban {
  cursor: default;
  pointer-events: none;
  opacity: 0.8;
}

.not-found {
  background-color: #f9f9f9;
  color: #444;
}
</style>
